Ext.ns('ru.proburo.oms.components.form');

/**
 * @class ru.proburo.oms.components.form.FilterableComboBox
 * 
 * @extends Ext.form.ComboBox
 */
ru.proburo.oms.components.form.FilterableComboBox = Ext.extend(Ext.form.ComboBox, {
    
    masterField: null,
    
    filterField: null,
    
    filterValue: null,
    
    relayClear:  false,
    
    autoSelect:  false, //TODO: implement
	
	autoLoad: 	 false,
    
    
    
    constructor: function(config)
	{
		config = config || {};
        
        Ext.applyIf(config, {			
            relayClear:    false,
            autoSelect:    false,
            
            mode:          'remote',
            triggerAction: 'all',
			editable:      false
		});
        
        ru.proburo.oms.components.form.FilterableComboBox.superclass.constructor.call(this, config);
	},
    
    initComponent : function()
    {
        ru.proburo.oms.components.form.FilterableComboBox.superclass.initComponent.call(this);
        
        this.addEvents('clear');
    },
    
    initEvents: function()
    {
        ru.proburo.oms.components.form.FilterableComboBox.superclass.initEvents.call(this);
        
        if(null !== this.masterField)
        {
            this.masterField.on('select',      this.filter, this);
            this.masterField.on('afterrender', this.filter, this);
            
            if(true == this.relayClear)
            {
                this.masterField.on('clear', this.clearValue, this);
            }
        }
    },
    
    clearValue: function()
    {
        ru.proburo.oms.components.form.FilterableComboBox.superclass.clearValue.call(this);
        
		delete this.filterValue;
		
        this.fireEvent('clear', this);
    },
    
    filter: function(field, value)
    {
        if(this.filterValue !== this.masterField.getValue())
        {
            if(true == this.relayClear)
            {
                this.clearValue();
            }
			
            this.filterValue = this.masterField.getValue();
            
            this.store.setBaseParam(this.filterField, this.filterValue);
			
			if(true === this.autoLoad)
			{
				this.store.load();
			}
			
			delete this.lastQuery;
        }        
    }
});